<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\Insurance\AICS\Response\AllAICSResponse;
use IDock\Printer\preInquiryPrinter;
use IDock\Printer\UploadFilePrinter;

class FloatRateResponse extends AllAICSResponse
{
    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = str_replace('&lt;','<',$body);
        $body = str_replace('&gt;','>',$body);
        $body = str_replace("'",'"',$body);
        $start = strpos($body,'<response>');
        $end = strpos($body,'</response>');
        $body = substr($body,$start + 10,intval($end - $start -10));
        if ($body) {
            libxml_disable_entity_loader(false);

            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);
            $errorCode = $body['code'] ?? '';
            $message = $body['msg'] ?? '未知错误';
            $this->setBody($body);
            if ($errorCode !== 'E0000') {
                $this->setError($message);
                return $this;
            }
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $Printer = new preInquiryPrinter();
        $data = $this->getBody();
        $Printer->argc['negotiatePrice'] = $data['val'] ?? 0;
        $this->printer = $Printer;

        return $this;
    }
}
